var Purchase = {
    balance: 10,
    currentPrice: 0,
    level: 3.0,
    aidou: 0,
    payPrice: 0,
    init: function () {
        if (isMacI4tools()){
            $('.footer').css('background','rgba(239, 241, 244, 1)')
        }
        window['cr'] = {
            receiveMessage: function (e) {
                var msg = JSON.parse(e);
                if (msg.event === 406) {
                    zz_send(JSON.stringify({event: 404}));
                }
            }
        };
        try {
            zz_send(JSON.stringify({event: 405, title: '购买验机分'}));
            // 兼容V8
            window._pcReady = function () {
                zz_send(JSON.stringify({event: 405, title: '购买验机分'}));
            };
        } catch (e) {
            console.log('设置标题', e);
        }
        ChargingAjax.getBalanceAndGoods(function (data) {
            if (data != null && data.data) {
                Purchase.balance = data.data.balance;
                $('#balance').text(Purchase.balance);
                Purchase.currentPrice = data.data.pricing[3].t1;
                $('#payPrice').text((data.data.pricing[3].t1 * 1).toFixed(1));
                // 设置选中值
                $('#one').attr('price', data.data.pricing[3].t1);
                $('#ten').attr('price', data.data.pricing[20].t1);
                $('#fifty').attr('price', data.data.pricing[50].t1);
                $('#hundred').attr('price', data.data.pricing[100].t1);
                // 设置价格
                $('#onePrice').text(data.data.pricing[3].t1 + '元');
                $('#tenPrice').text(data.data.pricing[20].t1 + '元');
                $('#fiftyPrice').text(data.data.pricing[50].t1 + '元');
                $('#hundredPrice').text(data.data.pricing[100].t1 + '元');
                // 设置折扣
                if (data.data.pricing[3].t2) {
                    $('#oneDiscount').text(data.data.pricing[3].t2).css('display', 'inline-block');
                }
                if (data.data.pricing[20].t2) {
                    $('#tenDiscount').text(data.data.pricing[20].t2).css('display', 'inline-block');
                }
                if (data.data.pricing[50].t2) {
                    $('#fiftyDiscount').text(data.data.pricing[50].t2).css('display', 'inline-block');
                }
                if (data.data.pricing[100].t2) {
                    $('#hundredDiscount').text(data.data.pricing[100].t2).css('display', 'inline-block');
                }
            }
        });
        Purchase.calcPayPrice();
        Purchase.bindAidouOperation();
        $('.item').click(function () {
            $('.item').removeClass('activated');
            $(this).addClass('activated');
            Purchase.currentPrice = $(this).attr('price');
            Purchase.level = $(this).attr('level');
            $('#payPrice').text((Purchase.currentPrice * 1).toFixed(1));
            $('#aidou').val((0).toFixed(1));
        });
        $('#back').click(function () {
            window.history.back();
        });
        $('#buy').click(function () {
            window.location.target = '_self';
            window.location.href = 'pay.html?' + '&level=' + Purchase.level + '&aidou=' + $('#aidou').val();
        });
    },
    calcPayPrice: function () {
        if (($('#aidou').val() - Purchase.currentPrice) > 0) {
            $('#aidou').val(Purchase.currentPrice);
        }
        $('#payPrice').text((Purchase.currentPrice - $('#aidou').val()).toFixed(1));
    },
    // 绑定爱豆操作
    bindAidouOperation: function () {
        $('#minus').click(function () {
            var value = $('#aidou').val() * 1;
            if (value <= 0) {
                return;
            }
            value -= 1;
            if (value < 0) {
                value = 0;
            }
            $('#aidou').val((value).toFixed(1));
            Purchase.calcPayPrice();
        });
        $('#aidou').bind('input', function () {
            if (Purchase.balance < 0) {
                $(this).val(0);
                Purchase.calcPayPrice();
                return;
            }
            var value = $(this).val();
            if (isNaN(value)) {
                $(this).val(0);
                return;
            }
            if (value >= Purchase.balance) {
                $(this).val(Purchase.balance);
                Purchase.calcPayPrice();
                return;
            }
            if (value >= Purchase.currentPrice) {
                $(this).val(Purchase.currentPrice);
                Purchase.calcPayPrice();
                return;
            }
            $('#aidou').val(value);
            Purchase.calcPayPrice();
        });
        $('#aidou').bind('blur', function () {
            var v = $(this).val() ? parseFloat($(this).val()).toFixed(1) : 0;
            $(this).val(v);
        });
        $('#plus').click(function () {
            var value = $('#aidou').val() * 1;
            if (value >= Purchase.balance) {
                return;
            }
            if (value >= Purchase.currentPrice) {
                $('#aidou').val(Purchase.currentPrice.toFixed(1));
                Purchase.calcPayPrice();
                return;
            }
            value += 1;
            $('#aidou').val((value).toFixed(1));
            if (value >= Purchase.balance) {
                $('#aidou').val(Purchase.balance);
            }
            Purchase.calcPayPrice();
        });
    }
};
